/**
 * Created by root on 2015/1/13.
 */

var mongoose = require('mongoose');
var Battle = mongoose.model('Battle');
var moment = require('moment');

function ReportService (){

}

ReportService.prototype.reportBattle = function (callback) {
    var reportData = '';
    Battle.find({
        status: 'F'
    }).populate({
        path: 'store owner users fleeUsers'
    }).exec(function(err, data){
        if (err) throw err;
        for (var i = 0, len = data.length; i < len; i++) {
            //题集名
            reportData += data[i]['store'].get('text') + ',';

            //创建人
            reportData += data[i]['owner'].get('name') + ',';

            //创建时间
            reportData += moment(data[i]['createTime']).format('YYYY年MM月DD日 HH时mm分ss秒') + ',';

            //开始时间
            reportData += moment(data[i]['start']).format('YYYY年MM月DD日 HH时mm分ss秒') + ',';

            //结束时间
            reportData += moment(data[i]['end']).format('YYYY年MM月DD日 HH时mm分ss秒') + ',';

            //参战人数
            var ustr = '"';
            var users = data[i]['users'];
            for (var j = 0, size = users.length; j < size; j++) {
                var user = users[j];
                if(user && user.name){
                    ustr += '姓名:' + user.name + ';得分:' + ((data[i].allScores && data[i].allScores[user.get('_id')]) || 0)+ '\n';
                }
            }
            ustr += '"';
            reportData += ustr + ',';

            //逃跑人数
            ustr = '"';
            var users = data[i]['fleeUsers'];
            for (var j = 0, size = users.length; j < size; j++) {
                var user = users[j];
                ustr += '姓名:' + user.get('name') + '\n';
            }
            ustr += '"';
            reportData += ustr + ',';

            reportData += '\n';
        }

        callback && callback(err, reportData);
    });
}

ReportService.prototype.reportUserBattle = function (sObjectId, callback) {
    var reportData = '';
    Battle.find({
        users: {
            $in: [sObjectId]
        }
    }).populate({
        path: 'store owner users fleeUsers'
    }).exec(function(err, data){
        if (err) throw err;
        for (var i = 0, len = data.length; i < len; i++) {
            //题集名
            reportData += data[i]['store'].get('text') + ',';

            //创建人
            reportData += data[i]['owner'].get('name') + ',';

            //创建时间
            reportData += moment(data[i]['createTime']).format('YYYY年MM月DD日 HH时mm分ss秒') + ',';

            //开始时间
            reportData += moment(data[i]['start']).format('YYYY年MM月DD日 HH时mm分ss秒') + ',';

            //结束时间
            reportData += moment(data[i]['end']).format('YYYY年MM月DD日 HH时mm分ss秒') + ',';

            //参战人数
            var ustr = '"';
            var users = data[i]['users'];
            for (var j = 0, size = users.length; j < size; j++) {
                var user = users[j];
                if(user && user.name){
                    ustr += '姓名:' + user.name + ';得分:' + ((data[i].allScores && data[i].allScores[user.get('_id')]) || 0)+ '\n';
                }
            }
            ustr += '"';
            reportData += ustr + ',';

            //逃跑人数
            ustr = '"';
            var users = data[i]['fleeUsers'];
            for (var j = 0, size = users.length; j < size; j++) {
                var user = users[j];
                ustr += '姓名:' + user.get('name') + '\n';
            }
            ustr += '"';
            reportData += ustr + ',';

            reportData += '\n';
        }

        callback && callback(err, reportData);
    });
}

module.exports = ReportService ;